package com.example.wszb.ui.fragment;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import android.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.wszb.ui.activity.CaseActivity;
import com.example.wszb.adapter.MyCaseAdapter;
import com.example.wszb.pojo.MyCaseItem;
import com.example.wszb.R;

import java.util.ArrayList;
import java.util.List;


public class HistoryFragment extends Fragment {
    private List<MyCaseItem> caselist = new ArrayList<>();
    @Nullable

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_history, container, false);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onActivityCreated( Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        inints();


        MyCaseAdapter adapter = new MyCaseAdapter(this.getContext(),R.layout.listview_item, caselist);
        ListView listView = (ListView)getActivity().findViewById(R.id.history_listview);
        listView.setAdapter((ListAdapter) adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            // 点击listview的某一项进入CaseActivity中
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MyCaseItem caseItem = caselist.get(position);
                if (caseItem.getName()!="笨笨"){
                    Intent intent = new Intent(getActivity(), CaseActivity.class);
                    startActivity(intent);
                }else {
                    Toast.makeText(getActivity(),"不是通讯录里的联系人( ´_ゝ`)", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
    private void inints() {  //开一个新线程，连接数据库获取该病人的所有以往病例
        for (int i=0; i<10;i++){
            MyCaseItem w1 = new MyCaseItem();
            w1.setImageId(R.mipmap.bg1);
            w1.setName("订阅号服务"+ i);
            w1.setContent("重庆科技学院:今晚，重科人用歌声献礼建党100周年!");
            caselist.add(w1);

        }

    }

}